package programmercarl.贪心.C4;

class Solution {
    public int maxSubArray(int[] nums) {
        int ans = Integer.MIN_VALUE;
        if (nums.length == 1) {return nums[0];}
        //如果子数组和为负数，则重新找到一个正数开始计算
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            count=count+nums[i];
            ans=Math.max(ans,count);
            if(count<0){
                count=0;
            }
        }
        return ans;
    }
}